package com.inspectortime.webapp.util;

import javax.servlet.http.HttpServletRequest;

import com.opensymphony.module.sitemesh.Decorator;
import com.opensymphony.module.sitemesh.Page;
import com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper;
import com.opensymphony.module.sitemesh.mapper.DefaultDecorator;

/**
 * Use a color-neutral decorator that works well with a customer's logo for
 * customer booking paths like /inspectors/[companyShortName]/schedule1.html.
 * However, make an exception if the query string parameter "fromSearch=true" is
 * set, thereby allowing us to retain standard IT skinning for search results.
 * 
 * @author mkvalsvik
 * 
 */
public class ParameterOverridingCompanyConfigDecoratorMapper extends
		ConfigDecoratorMapper {

	public Decorator getDecorator(HttpServletRequest request, Page page) {
		String thisPath = request.getServletPath();

		// Customer name in path?
		String companyShortName = RequestPathUtils
				.extractInspectorCompanyShortNameFromPath(request);
		if (RequestPathUtils.DEFAULT_INSPECTORS_VIEW_FOLDER
				.equals(companyShortName)) {

			// No. Therefore delegate to standard mapper.
			return super.getDecorator(request, page);
		}

		// From search result?
		if (HttpSessionUtils.isEnabledFromSearchFlag(request)) {

			// Yes. Retain original skinning by delegating to standard mapper
			return super.getDecorator(request, page);
		}

		// If we get here then assume we should apply customer logo-friendly
		// decorator with neutral colors
		Decorator result = null;
		final Decorator d = super.getDecorator(request, page);
		result = new DefaultDecorator("neutral-decorator",
				"/decorators/neutral-decorator.html", null) {
			public String getInitParameter(String paramName) {
				return d.getInitParameter(paramName);
			}
		};
		return result;
	}
}
